#include <Reg51.h>
#include "delay.h"
#include "LCD1602.h"
#include "menu.h"

void LCDShowService();
void Timer0Init(void);
void EXT_Init();

unsigned char sec,min,hour;
unsigned int timer0Cnt;
unsigned char state = 0;

void main()
{
	EXT_Init();
	LCDInit();
	Timer0Init();
	DelayXms(10);
	StartMenu();
	DelayXms(2000);
	LCDWriteCmd(LCD_SCREEN_CLR);
	while(1)
	{
		LCDShowStr(0,0,"  Current Time");
		LCDShowService();
	}
}


void LCDShowService()
{
	switch(state)
	{
		case 0:TR0 = 1;LCDShowSec();LCDShowMin();LCDShowHour();break;
		case 1:LCDShowSec();break;
		case 2:LCDShowMin();break;
		case 3:LCDShowHour();break;
		default:break;
	}
}

void EXT_Init()
{
	IT0 = 1;//下降沿触发方式 INT0
	IT1 = 1;//下降沿触发方式 INT1
	EX0 = 1;
	EX1 = 1;
}

void Timer0Init(void)		//1毫秒@11.0592MHz
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	ET0 = 1;
	EA = 1;
	TR0 = 1;		//定时器0开始计时
}


void EX0_ISR() interrupt 0
{
	TR0 = 0;
	state++;
	if(state > 3)
	{
		state = 0;
	}
}

void EX1_ISR() interrupt 2
{
	switch(state)
	{
		case 1:sec++;if(sec >= 60) sec = 0;break;
		case 2:min++;if(min >= 60) min = 0;break;
		case 3:hour++;if(hour >= 24) hour = 0;break;
		default:break;
	}
}

void timer0_ISR() interrupt 1
{
	TR0 = 0;
	timer0Cnt++;
	if (timer0Cnt >= 1000)
	{
		timer0Cnt = 0;
		sec++;
		if (sec >= 60)
		{
			sec = 0;
			min++;
			if (min >= 60)
			{
				min = 0;
				hour++;
				if(hour >= 24)
				{
					hour = 0;
				}
			}
		}
	}
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TR0 = 1;
}

